<?php
/**
* Site manager.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Config;
use Buan\ModelCriteria;
use Buan\ModelException;
class SiteManager extends ScribbleModelManager {

	/**
	* Return the model that represents the Site currently being viewed.
	*
	* If no Site can be found, or the system isn't installed yet, then NULL is
	* returned.
	*
	* If a Site is found, then the result is cached for use by all subsequent
	* calls to this method.
	*
	* @return SiteModel|NULL
	*/
	public static function getCurrentSite() {

		// Return cached result
		static $cSite = NULL;
		if($cSite!==NULL) {
			return $cSite;
		}

		// Find a Site that matches the currently active one
		$sitePrefix = Config::get('ext.Scribble.site.canonicalPrefix');
		$siteConfig = Config::get('ext.Scribble.site.config');
		$c = new ModelCriteria();
		$g = $c->addGroup(ModelCriteria::LOGIC_OR);
		$g->addClause(ModelCriteria::EQUALS, 'prefix', $sitePrefix);
		try {
			$sites = self::select('Site', $c)->asArray();
			if(!empty($sites)) {
				return $cSite = $sites[0];
			}
			else {
				return NULL;
			}
		}
		catch(ModelException $e) {
			return NULL;
		}
	}
}
?>